import { Controller, Post, UploadedFile, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { FileService } from './file.service';

@Controller('file')
export class FileController {
  constructor(
    private readonly fileService: FileService
  ) { }

  // 单文件上传
  @Post('upload')
  @UseInterceptors(FileInterceptor('file')) // 'file' 是前端字段名
  async uploadFile(@UploadedFile() file: Express.Multer.File) {
    return {
      originalname: file.originalname,
      filename: file.filename,
      url: this.fileService.getFileUrl(file.filename),
      size: file.size,
    };
  }

  // 多文件上传（最多5个）
  @Post('upload-multiple')
  @UseInterceptors(FilesInterceptor('files', 5)) // 'files' 是前端字段名
  async uploadMultiple(@UploadedFiles() files: Express.Multer.File[]) {
    return files.map(file => ({
      originalname: file.originalname,
      filename: file.filename,
      url: this.fileService.getFileUrl(file.filename),
      size: file.size,
    }));
  }
}
